#include "NArchiveFile.hpp"
#include "copystring.h"

DLL_API NArchiveFile::NArchiveFile(void): Offset(0), Size(0), Number(0), Data(NULL), FilenameA(NULL), FilenameW(NULL)
{
}

DLL_API NArchiveFile::NArchiveFile(const CHAR* fna, const WCHAR* fnw, DWORD s, INT64 o, DWORD n, BYTE* d, bool alloc): 
        Offset(o), Size(s), Number(n), Data(d), FilenameA(0), FilenameW(0)
{
    copystring(fna, &FilenameA);
    copystring(fnw, &FilenameW);
    if (alloc)
    {
        Alloc();
    }
}

DLL_API NArchiveFile::NArchiveFile(const NArchiveFile& naf): Offset(0), Size(0), Number(0), Data(NULL), FilenameA(NULL), FilenameW(NULL)
{
    *this = naf;
}

DLL_API NArchiveFile::~NArchiveFile(void)
{
    if (Data)
    {
        delete []Data;
    }
    if (FilenameA)
    {
        delete []FilenameA;
    }
    if (FilenameW)
    {
        delete []FilenameW;
    }
}

DLL_API NArchiveFile& NArchiveFile::operator =(const NArchiveFile &naf)
{
    Offset = naf.Offset;
    Size = naf.Size;
    Number = naf.Number;
    
    delete []Data;
    delete []FilenameA;
    delete []FilenameW;
    Data = NULL;
    FilenameA = NULL;
    FilenameW = NULL;

    copystring(naf.FilenameA, &FilenameA);
    copystring(naf.FilenameW, &FilenameW);

    if (naf.Data)
    {
        Data = new BYTE[Size];
        memcpy_s(Data, Size, naf.Data, naf.Size);
    }
    return *this;
}

DLL_API bool NArchiveFile::Alloc()
{
    return (Size>0) && (Data = new BYTE[Size]);
}